package com.sample.navigation.fragment.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.sample.navigation.R;
import com.sample.navigation.adapter.RecyclerViewAdapter;
import com.sample.navigation.adapter.TypedViewData;
import com.sample.navigation.fragment.Fragment2;
import com.sample.navigation.util.ScreenMetricUtil;
import com.sample.navigation.util.WindowStyleUtil;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class FragmentDialogBottom extends BottomSheetDialogFragment {
    private final String TAG = FragmentDialogBottom.class.getSimpleName();
    private Fragment2 parent;
    private View rootView;
    private RecyclerView recyclerView;
    private ArrayList<TypedViewData> spinList = new ArrayList<>();
    public FragmentDialogBottom(Fragment2 parent){
        this.parent = parent;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.dialog_bottom_sheet, null);
        initComponents();
        return rootView;
    }

    private void initComponents(){
        String[] spinArray = getResources().getStringArray(R.array.videoRecordTimeLimit);
        for (String str:spinArray){
            spinList.add(new TypedViewData(null, TypedViewData.TYPE_SPINNER_TEXT,
                    new int[]{},
                    new String[]{str},
                    new boolean[]{false}));
        }

        recyclerView = (RecyclerView)rootView.findViewById(R.id.rv_educational);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(spinList, getContext());
        adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {

            }
            @Override
            public void onSelect(View view, int position) {
                Log.d(TAG, "选中: " + spinList.get(position).getStringValue()[0]);
                setUnSelect(position);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);

        rootView.findViewById(R.id.btn_close)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
    }

    private void setUnSelect(int pos){
        for (int i=0;i!=spinList.size();i++){
            if(i == pos){
                spinList.get(i).setBoolValue(new boolean[]{true});
            }else{
                spinList.get(i).setBoolValue(new boolean[]{false});
            }
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                recyclerView.getAdapter().notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        spinList.clear();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //适用于老式手机,全面屏下请忽略
        if (getDialog() != null) {
            Window window = getDialog().getWindow();
            if (window != null) {
                window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                WindowStyleUtil.setWindowStyle(window, WindowStyleUtil.WS_NO_NAV, true);
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        final View view = getView();
        view.post(new Runnable() {
            @Override
            public void run() {
                View parent = (View) view.getParent();
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
                params.height = ScreenMetricUtil.getHeight() * 2 / 3;
                CoordinatorLayout.Behavior behavior = params.getBehavior();
                ((BottomSheetBehavior)behavior).setPeekHeight(params.height);
                parent.setLayoutParams(params);
                parent.setBackgroundColor(Color.TRANSPARENT);
            }
        });
    }
}
